package com.skyworth.web.admin.table.model;

import com.skyworth.web.admin.store.model.StoreModel;
import com.skyworth.web.admin.store.repository.StoreRepository;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.repository.UserRepository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.*;

/**
 * Created by cody on 2016/8/11.
 */
@Entity
@Component
@Table(name = "ff_table")
public class TableModel {
    /**
     * 台桌空闲状态
     */
    public static final int IDLE = 0;
    /**
     * 台桌使用中
     */
    public static final int USEING = 1;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Getter
    @Setter
    public long id;
    @Column
    @Getter
    @Setter
    public String name;

    @Column
    @Getter
    @Setter
    public int state;

    @Column(name = "store_id")
    public long storeId;

    @Column(name = "manager_id")
    @Getter
    @Setter
    public long managerId;

    @Column
    @Getter
    @Setter
    public int seating;

    @Column(name = "qrc_url")
    @Getter
    @Setter
    public String qrcUrl;

    @Column(name = "qrc_img")
    @Getter
    @Setter
    public String qrcImg;

    @Transient
    private static UserRepository userRepository;

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        TableModel.userRepository = userRepository;
    }

    @Transient
    private static StoreRepository storeRepository;

    @Autowired
    public void setStoreRepository(StoreRepository storeRepository) {
        TableModel.storeRepository = storeRepository;
    }

    public String getManager(){
        UserModel userModel = TableModel.userRepository.findOne(this.managerId);
        if(null != userModel){
            return userModel.getUsername();
        }else {
            return "未指定";
        }
    }

    public long getStoreId() {
        return storeId;
    }

    public void setStoreId(long storeId) {
        this.storeId = storeId;
    }

    public String getStoreName(){
        StoreModel storeModel = TableModel.storeRepository.findOne(this.storeId);
        if(null != storeModel){
            return storeModel.name;
        }else {
            return "skyworh_store";
        }
    }
}
